//
//  TopicViewController.m
//  XiangMuB
//
//  Created by 向修根 on 16/8/12.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "TopicViewController.h"
#import "TopicTableViewCell.h"
#import "TopocModel.h"
#import "TopicImageTableViewCell.h"
#import "Url_marco.h"
#import "GetDataHandle.h"
#import "TopicDetailsViewController.h"
#import "DataBaseHandle.h"
#import "MBProgressHUD.h"
#import <MJRefresh.h>

@interface TopicViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *TopTableView;

@property (strong,nonatomic) NSMutableArray *topicDataArray;//数据源数组

@property (assign,nonatomic) NSInteger currentPage;//下拉刷新所需

@property (copy,nonatomic) NSString *nameStr;//数据库中查询到的数据

@property (strong,nonatomic) MBProgressHUD *progressHUD;

@end

@implementation TopicViewController

//懒加载
- (NSMutableArray *)topicDataArray{
    if (_topicDataArray == nil) {
        _topicDataArray = [NSMutableArray array];
    }
    return _topicDataArray;
}


- (void)viewWillAppear:(BOOL)animated{
//    页面即将出现时显示tabbar
    self.tabBarController.tabBar.hidden = NO;
    [self.TopTableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.TopTableView.delegate = self;
    self.TopTableView.dataSource = self;
    self.TopTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
//    注册
    [self.TopTableView registerNib:[UINib nibWithNibName:@"TopicTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell"];
    [self.TopTableView registerNib:[UINib nibWithNibName:@"TopicImageTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"imageCell"];
    


//    //    下拉加载
    self.TopTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.topicDataArray removeAllObjects];
        [self getSession];
    }];
    [self.TopTableView.mj_header beginRefreshing];
    
    //    上拉刷新
    self.TopTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self refresh];
    }];
    

    
    //    数据库相关
    [[DataBaseHandle shareDataBaseHandle]openDatabase];
    NSArray *array1 = [[DataBaseHandle shareDataBaseHandle]select];
    for (NSDictionary *dic in array1) {
        self.nameStr = dic[@"name"];
        
    }

//    取消tableview 中的换行线
    self.TopTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
}

//数据解析
- (void)getSession{
   
    [[GetDataHandle sharedDataHandle]getDataWithUrlString:TOPIC_URL withGetDataBlock:^(NSData *data) {
        NSError *error = nil;
        NSDictionary *di = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
        NSDictionary *dic = di[@"data"];
        NSArray *array1 = dic[@"subjectList"];
 
        for (NSDictionary *dic in array1) {
            TopocModel *model = [TopocModel new];
            [model setValuesForKeysWithDictionary:dic];
            [self.topicDataArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.TopTableView.mj_header endRefreshing];
            [self.TopTableView reloadData];
        });
        
    }];
}

//上拉刷新
- (void)refresh{
    
     self.currentPage += 10;
    NSLog(@"%ld",self.currentPage);
    NSString *str = [NSString stringWithFormat:@"http://topic.comment.163.com/topic/list/subject/%ld-10.html",self.currentPage];
    [[GetDataHandle sharedDataHandle]getDataWithUrlString:str withGetDataBlock:^(NSData *data) {
        NSError *error = nil;
        NSDictionary *di = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
        NSDictionary *dic = di[@"data"];
        NSArray *array1 = dic[@"subjectList"];
        
        for (NSDictionary *dic in array1) {
            TopocModel *model = [TopocModel new];
            [model setValuesForKeysWithDictionary:dic];
            [self.topicDataArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.TopTableView.mj_footer endRefreshing];
            [self.TopTableView reloadData];
        });
        
    }];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.topicDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TopocModel *model = self.topicDataArray[indexPath.row];
    
    if (model.talkContent == nil) {
        TopicImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imageCell" forIndexPath:indexPath];
        
//        点击cell不变色
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        

        //夜间模式
        [cell NightWithType:UIViewColorTypeNormal];
          [cell.cellView NightWithType:UIViewColorTypeClear];
        cell.model = model;
        

        
         return cell;
    }else{
      TopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        
        //        点击cell不变色
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
     
        
        cell.model = model;
        //夜间模式
        [cell NightWithType:UIViewColorTypeNormal];
        //夜间模式
         [cell.cellView NightWithType:UIViewColorTypeClear];
         return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TopocModel *model = self.topicDataArray[indexPath.row];
    if (model.talkContent == nil){
        return 230;
    }else{
        return 200;
    }
    

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    
    TopocModel *model = self.topicDataArray[indexPath.row];
    TopicDetailsViewController *topic = [TopicDetailsViewController new];
    topic.strID = model.subjectId;
    [self showViewController:topic sender:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
